package Demo1;

import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 34255
 * Date: 2024-07-13
 * Time: 10:41
 */
public class MyLinkedList implements IList{

    //头插法
    @Override
    public void addFirst(int data) {
        ListNode newNode = new ListNode(data);
        if(head == null){
            head = last = newNode;
            return;
        }
        head.prev = newNode;
        newNode.next = head;
        head = newNode;
    }

    //尾插法
    @Override
    public void addLast(int data) {
        ListNode newNode = new ListNode(data);
        if(last == null){
            last = head = newNode;
            return;
        }
        newNode.prev = last;
        last.next = newNode;
        last = newNode;
    }

    //检查index位置是否合法
    public void checkIndex(int index) throws IndexIllegality{
        if(index < 0 || index > size()){
            throw new IndexIllegality("index位置是否合法");
        }
    }

    //检查链表是否为空
    public void checkEmpty(){
        if(head == null){
            throw new isEmpty("链表为空");
        }
    }
    //任意位置插入,第一个数据节点为0号下标
    @Override
    public void addIndex(int index, int data) {
        try{
            //检查链表是否为空
            checkEmpty();
            //检查index合法性
            checkIndex(index);

            if(index == 0){
                addFirst(data);
                return;
            }

            if(index == size()){
                addLast(data);
                return;
            }

            ListNode newNode = new ListNode(data);
            //找到指定下标节点
            ListNode cur = head;
            while(index != 0){
                cur = cur.next;
                index--;
            }

            //修改
            newNode.next = cur;
            newNode.prev = cur.prev;
            cur.prev.next = newNode;
            cur.prev = newNode;

        }catch(IndexIllegality e){
            e.printStackTrace();
        }
    }

    //查找是否包含关键字key是否在链表当中
    @Override
    public boolean contains(int key) {
        ListNode cur = head;
        while(cur != null){
            if(cur.val == key){
               return true;
            }
            cur = cur.next;
        }
        return false;
    }

    //删除第一次出现关键字为key的节点
    @Override
    public void remove(int key) {
        try{
            //链表为空
            checkEmpty();

            //先找到该节点
            ListNode cur = head;
            while(cur != null){
                if(cur.val == key){
                    break;
                }
                cur = cur.next;
            }

//            //头节点
//            if(cur == head){
//                cur.next.prev = cur.prev;
//                head = cur.next;
//            }
//
//            //尾节点
//            if(cur == last){
//                cur.prev.next = cur.next;
//                last = cur.prev;
//            }

            if(head == last){//一个节点
                head = last = null;
                return;
            }else{
                if(cur == head){//头节点
                    cur.next.prev = cur.prev;
                    head = cur.next;
                }else{//尾节点+中间节点
                    if(cur != last){
                        cur.next.prev = cur.prev;
                    }
                    cur.prev.next = cur.next;
                    if(cur == last){
                        last = cur.prev;
                    }
                }
            }



        }catch(isEmpty e){
            e.printStackTrace();
        }
    }

    //删除所有值为key的节点
    @Override
    public void removeAllKey(int key) {
        try{
            //链表为空
            checkEmpty();
            //找到等于该值的节点
            ListNode cur = head;
            while(cur != null){
                if(cur.val == key){
                    if(head == last){//一个节点
                        head = last = null;
                        return;
                    }else{//会漏掉只有一个节点情况
                        if(cur == head){//头节点
                            cur.next.prev = cur.prev;
                            head = cur.next;
                        }else{//尾节点+中间节点
                            if(cur != last){
                                cur.next.prev = cur.prev;
                            }
                            cur.prev.next = cur.next;
                            if(cur == last){
                                last = cur.prev;
                            }
                        }
                    }
                }
                cur = cur.next;
            }
        }catch(isEmpty e){
            e.printStackTrace();
        }
    }

    //得到链表的长度
    @Override
    public int size() {
        ListNode cur = head;
        int len = 0;
        while(cur != null){
            len++;
            cur = cur.next;
        }
        return len;
    }

    //打印链表
    @Override
    public void display() {
        ListNode cur = head;
        while(cur != null){
            System.out.print(cur.val + " ");
            cur = cur.next;
        }
        System.out.println();
    }

    //清空链表
    @Override
    public void clear() {
        ListNode cur = head;
        while(cur != null){
            ListNode curN = cur.next;
            cur.prev = cur.next = null;
            cur = curN;
        }
        head = last = null;
    }

    //节点
    static class ListNode{
        public int val;
        public ListNode prev;
        public ListNode next;

        public ListNode(int data){
            this.val = data;
        }
    }

    //头尾节点
    public ListNode head;
    public ListNode last;


}
